﻿using System;
using System.Collections.Generic;
using DataEntities;
using NUnit.Framework;
using Repositories.Repository;
using Repositories.UoW;
namespace TestRepositories
{
    [TestFixture]
    public class TestHistoryAbonentPayRepository : ITestRepository
    {
        private IHistoryAbonentPayRepository _repository;
        private RepositoryFactory _repositoryFactory;
        private HistoryAbonentPay _historyAbonentPay;
        private IDogovorRepository _dogovorRepository;

        [SetUp]
        public void SetUp()
        {
            SessionFactory.Init();
            SessionFactory.GetSession().Flush();
            _repositoryFactory = new RepositoryFactory();
            _repository = _repositoryFactory.GetHistoryAbonentPayRepository();
            _dogovorRepository = _repositoryFactory.GetDogovorRepository();

            _historyAbonentPay=new HistoryAbonentPay(new DateTime(2011,08,05),1000,_dogovorRepository.Get(3));

        }

        [TearDown]
        public void CleanUp()
        {
            //SessionFactory.GetSession().Close();
        }

        [Test]
        public void TestGet()
        {
            Assert.AreEqual(5, _repository.Get(5).Id);
            Assert.AreEqual(new DateTime(2008,3,1), _repository.Get(5).DateChange);
            Assert.AreEqual(10, _repository.Get(5).AbonentPayValue);
            Assert.AreEqual(331, _repository.Get(5).Dogovor.Id);
        }

        [Test]
        //[Ignore]
        public void TestGetList()
        {
            Assert.AreEqual(1972, _repository.GetList().Count);
        }

        [Test]
        public void TestInsert()
        {
            Assert.AreEqual(1, _repository.Insert(_historyAbonentPay));
            Assert.AreEqual(1000,_repository.Get(_historyAbonentPay.Id).AbonentPayValue);
            Assert.AreEqual(new DateTime(2011,08,05),_repository.Get(_historyAbonentPay.Id).DateChange);
            Assert.AreEqual(3, _repository.Get(_historyAbonentPay.Id).Dogovor.Id);
        }

        [Test]
        public void TestUpdate()
        {
            _historyAbonentPay = _repository.Get(10);
            _historyAbonentPay.AbonentPayValue += 1000;
            Assert.AreEqual(1, _repository.Update(_historyAbonentPay));
            Assert.AreEqual(1020,_repository.Get(10).AbonentPayValue);
        }

        [Test]
        [Ignore]
        public void TestDelete()
        {
            _historyAbonentPay=new HistoryAbonentPay(10,new DateTime(2008,1,1),20,_dogovorRepository.Get(25) );
            Assert.AreEqual(1, _repository.Delete(_historyAbonentPay));
        }

        [Test]
        //[Ignore]
        public void TestEquals()
        {
            _historyAbonentPay=new HistoryAbonentPay(6,new DateTime(2005,05,01),10,_dogovorRepository.Get(17) );
            Assert.AreEqual(_historyAbonentPay, _repository.Get(6));
        }

        [Test]
        //[Ignore]
        public void TestInsertList()
        {
            IList<HistoryAbonentPay> list = new List<HistoryAbonentPay>();
            list.Add(new HistoryAbonentPay(new DateTime(2011,12,12),1000,_dogovorRepository.Get(3) ));
            list.Add(new HistoryAbonentPay(new DateTime(2011, 12, 12), 1000, _dogovorRepository.Get(3)));
            list.Add(new HistoryAbonentPay(new DateTime(2011, 12, 12), 1000, _dogovorRepository.Get(3)));
            Assert.AreEqual(3, _repository.InsertList(list));
        }

        [Test]
        //[Ignore]
        public void UpdateList()
        {
            IList<HistoryAbonentPay> list = new List<HistoryAbonentPay>();
            list.Add(_repository.Get(8));
            list.Add(_repository.Get(11));
            list.Add(_repository.Get(14));
            list.Add(_repository.Get(21));

            foreach (HistoryAbonentPay item in list)
            {
                item.AbonentPayValue += 1000;
            }

            Assert.AreEqual(4, _repository.UpdateList(list));
        }

        [Test]
        [Ignore]
        public void TestDeleteList()
        {
            IList<HistoryAbonentPay> list = new List<HistoryAbonentPay>();
            list.Add(new HistoryAbonentPay(1,new DateTime(2005,3,1),10,_dogovorRepository.Get(7) ));
            list.Add(new HistoryAbonentPay(5, new DateTime(2008, 3, 1), 10, _dogovorRepository.Get(331)));
            list.Add(new HistoryAbonentPay(6, new DateTime(2005, 5, 1), 10, _dogovorRepository.Get(17)));
            list.Add(new HistoryAbonentPay(7, new DateTime(2006, 5, 1), 15, _dogovorRepository.Get(17)));
            Assert.AreEqual(4, _repository.DeleteList(list));
        }
    }
}